{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from jqdatasdk import *\n",
    "from pyecharts import  Kline,Bar,Grid"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "首先我们先应JQDATA 的活动演示一下如何调用pyecharts 画图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "auth success\n"
     ]
    }
   ],
   "source": [
    "auth('acc','password')\n",
    "data=get_price('000001.XSHE')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "先打印下 data\n",
    "我们可以看到  jqdata返回的格式是 一个单index的Dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>money</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-01-05</th>\n",
       "      <td>10.53</td>\n",
       "      <td>10.55</td>\n",
       "      <td>10.72</td>\n",
       "      <td>10.27</td>\n",
       "      <td>434357784.0</td>\n",
       "      <td>4.565388e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-06</th>\n",
       "      <td>10.44</td>\n",
       "      <td>10.39</td>\n",
       "      <td>10.79</td>\n",
       "      <td>10.24</td>\n",
       "      <td>328971478.0</td>\n",
       "      <td>3.453446e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-07</th>\n",
       "      <td>10.25</td>\n",
       "      <td>10.19</td>\n",
       "      <td>10.42</td>\n",
       "      <td>10.08</td>\n",
       "      <td>258163619.0</td>\n",
       "      <td>2.634796e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-08</th>\n",
       "      <td>10.21</td>\n",
       "      <td>9.85</td>\n",
       "      <td>10.25</td>\n",
       "      <td>9.81</td>\n",
       "      <td>213761656.0</td>\n",
       "      <td>2.128003e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-09</th>\n",
       "      <td>9.81</td>\n",
       "      <td>9.93</td>\n",
       "      <td>10.45</td>\n",
       "      <td>9.69</td>\n",
       "      <td>380916192.0</td>\n",
       "      <td>3.835378e+09</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             open  close   high    low       volume         money\n",
       "2015-01-05  10.53  10.55  10.72  10.27  434357784.0  4.565388e+09\n",
       "2015-01-06  10.44  10.39  10.79  10.24  328971478.0  3.453446e+09\n",
       "2015-01-07  10.25  10.19  10.42  10.08  258163619.0  2.634796e+09\n",
       "2015-01-08  10.21   9.85  10.25   9.81  213761656.0  2.128003e+09\n",
       "2015-01-09   9.81   9.93  10.45   9.69  380916192.0  3.835378e+09"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在画K线图的时候, 我们需要的是上下两个部分\n",
    "![](http://pic.yutiansut.com/jq.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 因此我们初始化2个部分\n",
    "\n",
    "kline=Kline(width=1360, height=700, page_title='000001')\n",
    "\n",
    "bar = Bar()\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "然后我们需要对数据进行初步的处理:\n",
    "\n",
    "- 首先处理横坐标轴(时间轴)\n",
    "- 分别处理 价格轴/量轴"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "# 做横轴的处理\n",
    "datetime = np.array(data.index.map(str))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "ohlc = np.array(data.loc[:, ['open', 'close', 'low', 'high']])\n",
    "vol = np.array(data.volume)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "将数据加载到kline和bar中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "kline.add('000001', datetime, ohlc, mark_point=[\n",
    "      \"max\", \"min\"], is_datazoom_show=False, datazoom_orient='horizontal')\n",
    "\n",
    "bar.add('000001', datetime, vol,\n",
    "        is_datazoom_show=True,\n",
    "        datazoom_xaxis_index=[0, 1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用Grid组合两个图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "grid = Grid(width=1360, height=700, page_title='QUANTAXIS')\n",
    "grid.add(bar, grid_top=\"80%\")\n",
    "grid.add(kline, grid_bottom=\"30%\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "grid.render('000001_plot.html')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用 webbrowser 打开并渲染这个图\n",
    "![](http://pic.yutiansut.com/QQ%E6%88%AA%E5%9B%BE20181109203715.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import webbrowser\n",
    "webbrowser.open('000001_plot.html')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n以上是jqdata和pyecharts的结合, 不过QUANTAXIS已经对于这些进行了封装  只需要转化jqdata获取回来的数据为QADataStruct即可\\n'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "以上是jqdata和pyecharts的结合, 不过QUANTAXIS已经对于这些进行了封装  只需要转化jqdata获取回来的数据为QADataStruct即可\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "import QUANTAXIS as QA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "qads=QA.QAData.QA_DataStruct_Stock_day(data.assign(date=data.index,code='000001').set_index(['date','code']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "< QA_DataStruct_Stock_day with 1 securities >"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qads"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "QUANTAXIS>> The Pic has been saved to your path: .\\QA_stock_day_000001_bfq.html\n"
     ]
    }
   ],
   "source": [
    "qads.plot('000001')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
